package com.ssm.pre.util;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.ssm.pre.bean.Template;
import com.ssm.pre.bean.TemplateParam;
import com.ssm.util.Constants;
import com.ssm.wechat.service.WeChatApiUtil;

import net.sf.json.JSONObject;

public class TemplateUtil {

	 public static boolean sendTemplateMsg(Template template) throws Exception{  
	        boolean flag=false;  
	        //url 为模板消息请求接口 和 微信token拼接
//	        String url = Constants.TEMPLATE_API + WeixinUtil.getAccessToken("wx85917bcac369a4a3","c7753019d223bfd4fbab4f1b9bf8af12").getToken();
	        String url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="+WeixinUtil.getAccessToken(Constants.APP_ID,Constants.APPSECRET).getToken();
	        String response=WeChatApiUtil.httpsRequestToString(url, "POST", toJSON(template));  
	        JSONObject jsonResult = JSONObject.fromObject(response);
	        if(jsonResult!=null){  
	            int errorCode=jsonResult.getInt("errcode");  
	            String errorMessage=jsonResult.getString("errmsg");  
	            if(errorCode==0){  
	                flag=true;  
	                System.out.println("模板消息发送成功");
	            }else{  
	                System.out.println("模板消息发送失败:"+errorCode+","+errorMessage);  
	                flag=false;  
	            }  
	        }  
	        return flag;  
	} 

	   public static void Template(String ToUser,String type,String name) throws Exception {
	       	List<TemplateParam> paras=new ArrayList<TemplateParam>();  
	        paras.add(new TemplateParam("first","您好，你有新的"+type,"#FF3333"));  
	        paras.add(new TemplateParam("keyword1",name,"#0044BB"));  
	        paras.add(new TemplateParam("keyword2",type,"#0044BB"));
	        //paras.add(new TemplateParam("remark","请尽快处理！","#0044BB"));

	        Template tem = new Template(ToUser,paras,name,Constants.TEMPLATE_SIGN_ID);  
	        boolean result=sendTemplateMsg(tem);  
	        System.out.println("調用成功");
	   } 

	   //住户信息登记发送模板消息
	   public static void Template(Map<String, Object> map) throws Exception {
		    System.out.println(map);
		   	String familyType = "";
		    if(map.get("familyType").toString().equals("未审核")){
		    	familyType = "正在审核中，请耐心等待！";  
		   	}else if(map.get("familyType").toString().equals("审核通过")){
		   		familyType = "已经审核通过，祝您生活愉快！";  
		   	}else{
		   		familyType = "审核失败，请重新登记！";  
		   	}
		    //模板消息的内容
			List<TemplateParam> paras=new ArrayList<TemplateParam>(); 
			paras.add(new TemplateParam("first","尊敬的"+map.get("familyName").toString()+":\\n\\n您登记的信息认证申请结果如下：\\n",""));
			paras.add(new TemplateParam("keyword1",map.get("familyName").toString(),""));
			paras.add(new TemplateParam("keyword2",map.get("hInfo").toString(),""));
			paras.add(new TemplateParam("keyword3",familyType+"\\n",""));
			paras.add(new TemplateParam("remark","感谢您对我们服务的支持和监督，如有疑问请拨打热线0394-8581688，\\n祝您生活愉快。\\n",""));
			Template tem = new Template(map.get("openId").toString(),paras,map.get("familyName").toString(),Constants.TEMPLATE_SIGN_ID);  
	        boolean result=sendTemplateMsg(tem);
	        if(result){
	        	System.out.println("調用成功");
	        }else{
	        	System.out.println("調用失败");
	        }
	        
	   }
	   
	   //意见反馈
	   public static void Template2(Map<String,Object> map) throws Exception {
		    String optionType = map.get("optionType").toString();
		    if(optionType.equals("投诉")){
		    	optionType = "问题，我们会及时处理，感谢您的建议！";
		    }else if(optionType.equals("建议")){
		    	optionType = "建议，我们会予以考虑，感谢您的建议！";
		    }else{
		    	optionType = "表扬，我们会虚心接受，并再接再厉，感谢您的表扬！";
		    }
	       	List<TemplateParam> paras=new ArrayList<TemplateParam>();  
	        paras.add(new TemplateParam("first","亲爱的"+map.get("familyName").toString()+"\\n",""));  
	        paras.add(new TemplateParam("keyword1",map.get("optionText").toString(),""));  
	        paras.add(new TemplateParam("keyword2","您所提的"+optionType+"\\n",""));  
	        paras.add(new TemplateParam("remark","感谢您对我们工作的支持，如有疑问请拨打热线0394-8581688，\\n祝您生活愉快！",""));
//	        paras.add(new TemplateParam("remark","详情","#A9A9A9"));

	        Template tem = new Template(map.get("openId").toString(),paras,map.get("familyName").toString(),Constants.TEMPLATE_OPTION_ID);  
	        boolean result=sendTemplateMsg(tem);  
	        if(result){
	        	System.out.println("調用成功");
	        }else{
	        	System.out.println("調用失败");
	        }
	   } 
	   
	   //故障报修
	   public static void Template3(Map<String,Object> map) throws Exception {
		    String faultState=map.get("faultState").toString();
		    String serviceName = "";
		    String info = "";
		    if(faultState.equals("1")){
		    	faultState = "待分配";
		    	serviceName = "待分配";
		    	info = "您的报修请求，我们将尽快安排人进行维修，请耐心等待.";
		    }else if(faultState.equals("2")){
		    	faultState = "已分配";
		    	serviceName = map.get("serviceName").toString();
		    	info = "您的报修请求，我们已经安排维修工进行处理，请耐心等待.";
		    }else if(faultState.equals("3")){
		    	faultState = "已完成";
		    	serviceName = map.get("serviceName").toString();
		    	info = "您的报修请求，已经维修完成，如有问题请联系物业公司.";
		    }
		    String repairDate = map.get("repairDate").toString();
		    if(repairDate.equals("0")){
		    	repairDate = "待定";
		    }else{
		    	repairDate = repairDate+" "+map.get("repairDateDoor").toString();
		    }
		   
	       	List<TemplateParam> paras=new ArrayList<TemplateParam>();  
	        paras.add(new TemplateParam("first","亲爱的"+map.get("familyName").toString()+",\\n"+info+"\\n",""));  
	        paras.add(new TemplateParam("keyword1",map.get("repairNumber").toString(),""));  
	        paras.add(new TemplateParam("keyword2",map.get("hInfo").toString(),""));
	        paras.add(new TemplateParam("keyword3",map.get("repairTitle").toString(),""));
	        paras.add(new TemplateParam("keyword4",faultState,""));
	        paras.add(new TemplateParam("keyword5","维修工程师"+serviceName+"\\n",""));
	        paras.add(new TemplateParam("remark","感谢您对我们工作的支持，如有疑问请拨打热线0394-8581688，\\n祝您生活愉快！",""));

	        Template tem = new Template(map.get("openId").toString(),paras,map.get("familyName").toString(),Constants.TEMPLATE_REPAIR_ID);  
	        boolean result=sendTemplateMsg(tem);  
	        if(result){
	        	System.out.println("調用成功");
	        }else{
	        	System.out.println("調用失败");
	        }
	   } 
	   
	   //付款成功通知
	   public static void Template4(Map<String,Object> map) throws Exception {
	       	List<TemplateParam> paras=new ArrayList<TemplateParam>();  
	        paras.add(new TemplateParam("first","尊敬的"+map.get("familyName").toString()+"您好，本次缴费已成功，订单如下：\\n",""));  
	        paras.add(new TemplateParam("keyword1",map.get("hInfo").toString(),""));  
	        paras.add(new TemplateParam("keyword2",map.get("orderType").toString(),""));
	        paras.add(new TemplateParam("keyword3",map.get("nowDate").toString(),""));
	        paras.add(new TemplateParam("keyword4",map.get("payFee").toString()+"\\n",""));
	        paras.add(new TemplateParam("remark","感谢您的使用，如有疑问请拨打热线0394-8581688，\\n祝您生活愉快！\\n",""));

	        Template tem = new Template(map.get("openId").toString(),paras,map.get("familyName").toString(),Constants.TEMPLATE_LIFE_ID);  
	        boolean result=sendTemplateMsg(tem);  
	        if(result){
	        	System.out.println("調用成功");
	        }else{
	        	System.out.println("調用失败");
	        }
	   }
	   
	   public static String toJSON(Template template) {  
	       StringBuffer buffer = new StringBuffer();  
	       buffer.append("{");  
	       buffer.append(String.format("\"touser\":\"%s\"", template.getToUser())).append(",");  
	       buffer.append(String.format("\"template_id\":\"%s\"", template.getTemplateId())).append(",");  
	       //消息连接
	       //buffer.append(String.format("\"url\":\"%s\"", this.url)).append(",");  
	       buffer.append(String.format("\"topcolor\":\"%s\"", template.getTopColor())).append(",");  
	       buffer.append("\"data\":{");  
	       TemplateParam param = null;  
	       for (int i = 0; i < template.getTemplateParamList().size(); i++) {  
	            param = template.getTemplateParamList().get(i);  
	           // 判断是否追加逗号  
	           if (i < template.getTemplateParamList().size() - 1){  
	               buffer.append(String.format("\"%s\": {\"value\":\"%s\",\"color\":\"%s\"},", param.getName(), param.getValue(), param.getColor()));  
	           }else{  
	               buffer.append(String.format("\"%s\": {\"value\":\"%s\",\"color\":\"%s\"}", param.getName(), param.getValue(), param.getColor()));  
	           }  
	       }  
	       buffer.append("}");  
	       buffer.append("}");
	       return buffer.toString();  
	   }

	   public static void main(String args[]) throws Exception{
	       //第一个参数为用户关注微信后的唯一标识OpenId,第二个和第三个参数根据自己需要传递
	       Template("oCYVEv4f9JKAU6iZmVocGMDxl1ik","调休申请","马培淞");
	   }
	   
	
	}
